import dayjs from 'dayjs'; // 引入dayjs插件
let counts = null
Page({
    data: {
        isShow: false, // 控制年月弹框
        currentDate: new Date().getTime(),
        minDate: new Date(2023,0 ,1).getTime(),// 初始化当月最小时间
        maxDate: new Date(2023, 0, 31).getTime(),//初始化当月最大时间
        year: '2022年',
        month: '6月',
    },
    // 点击跳转会前一页
    onClickLeft() {
        wx.navigateBack({
            delta: 1
        })
    },
    // 点击弹出年月
    onClickShow() { 
        this.setData({
            isShow: true,
            currentDate: counts.valueOf()
        })
    },
    // 关闭年月弹框
    closeDateLayer() {
        this.setData({
            isShow: false,
        })
    },

    //确认按钮
    onConfirm(e){
        this.setData({
            isShow: false, 
        })
        this.getDate(dayjs(e.detail))
    },
    onLoad() {
       const year = (new Date()).getFullYear()
       const month = (new Date()).getMonth()
    //    console.log(year, month)
       counts = dayjs().month(month).year(year)
       this.getDate(counts)
    },
    //将dayjs中的方法封装
    getDate(counts) {
        const year = counts.year()
        const month = counts.month()
        const minDate = counts.startOf('month').valueOf()
        const maxDate = counts.endOf('month').valueOf() 
        // console.log(year, month, minDate, maxDate)
        this.setData({
            year, month, minDate, maxDate
        })
    },
    // 处理月份加减 dayjs中的方法
    saveMonth({mark:{type}}) {
        // console.log(type)
        if(type === 'down') {
            counts = counts.subtract(1, 'month')
            // console.log(counts)
            this.getDate(counts)
        }else{
            counts = counts.add(1, 'month')
            // console.log(counts)
            this.getDate(counts)
        }
    }
})